/*
    This is a set of libraries to assist in OpenGl and Other Applications
    Copyright (C) 2008  The Modular Engine Team

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/// \file WindowModule.h
/// \brief Contains OS specific classes for opening a window and 
/// setting it up for the GraphicsModule.

#ifndef WINDOWMODULE_H
#define WINDOWMODULE_H
#include <windows.h>
#include "EngineCore.h" 
#include "Math.h"
#include "InputModule.h"

#undef CLASSDECL
#ifdef WINDOWMODULE
	#define CLASSDECL __declspec(dllexport)
#else
	#pragma comment( lib, "WindowModule.lib" )
	#define CLASSDECL __declspec(dllimport)
#endif

namespace ModularEngine
{

	// Can't use this specific class in Linux/Mac
	#if WINDOWS 

	/// Windows Version of WindowModule
	class CLASSDECL WindowModule: BASE(WindowModule)
	{
	protected:
		HWND mhWnd;
		MSG msg; 
		WNDCLASSEX wcx;
		/// Classname of window
		char *mpClassName; 
		/// Window Title
		char *mpWindowName; 

		// Soon To be Deprecated
		bool mbLButtonDown; // Is left button down
		bool mbMButtonDown; // Is middle button down
		bool mbRButtonDown; // Is right button down

		LPCSTR msMouseName; // Change mouse to whatever we need.

		// Sends mouse down events to functions
		virtual void mouseSend( UINT down );

		// Sends keyboard input events to functions
		virtual void keyboardSend( UINT nKey );

		// Window position and dimensions
		int mX, mY, mWidth, mHeight;

	public:
		// Initialize the window
		int initialize();
		// Message handling system
		int run();

		KMFunctor *keyboardSendF;
		KMFunctor *mouseSendF;
		

		void setClassName( char *pClassName )
		{
			delete [] mpClassName;
			mpClassName = new char[strlen(pClassName)+1];
			strcpy( mpClassName, pClassName );
		}
		
		void setWindowName( char *pWindowName )
		{
			delete [] mpWindowName;
			mpWindowName = new char[strlen(pWindowName)+1];
			strcpy( mpWindowName, pWindowName );
		}

		void setDimensions( int x = 0, int y = 0, int width = 800, int height = 600 )
		{
			mX = x;
			mY = y;
			mWidth = width;
			mHeight = height;
		}

		Vector2d getDimensions()
		{
			Vector2d dims;
			dims.x = mWidth;
			dims.y = mHeight;
			return dims;
		}

		void setMouse( LPCSTR pMouse )
		{
			msMouseName = pMouse;
		}

		HWND getHwnd()
		{
			return mhWnd;
		}

		// Get's mouse position relative to window position
		Vector2d getMouse();

		// Finds out if mouse key is down.  0 being left button
		bool isDown( UINT uMouse );	

		WindowModule();
		~WindowModule();
		
	public:
		// Use of WndProc and Static WndProc for message loop
		static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
		LRESULT WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);


	};
	#endif // #if WINDOWS
}

#endif
